package com.wm.machine.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import java.lang.reflect.Method;

/**
 * Author： cxp
 * CreatTime： 2018/1/26
 * ContactInfo：
 * Description: Screen tools
 */
public final class ScreenUtils {
  private ScreenUtils() {
    throw new UnsupportedOperationException("ScreenUtils cannot be instantiated");
  }

  /**
   * 获取屏幕的宽度
   *
   * @param context 上下文context
   * @return 屏幕的宽度（px）
   */
  public static int getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
  }

  /**
   * 获取屏幕的高度
   *
   * @param context 上下文context
   * @return 屏幕的高度
   */
  public static int getScreenHeight(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.heightPixels;
  }

  /**
   * 获取实际屏幕高度
   *
   * @param context 上下文context
   * @return 实际屏幕高度
   */
  public static int getRealScreenHight(Context context) {
    int high = 0;
    Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    try {
      Class displayClass = Class.forName("android.view.Display");
      Method method = displayClass.getMethod("getRealMetrics", DisplayMetrics.class);
      method.invoke(display, metrics);
      high = metrics.heightPixels;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return high;
  }

  /**
   * 获取虚拟按键区域的高度
   *
   * @param context 上下文context
   * @return 虚拟键盘的高度
   */
  public static int getNavigationAreaHeight(Context context) {
    int realScreenHeight = getRealScreenHight(context);
    int screenHeight = getScreenHeight(context);
    return realScreenHeight - screenHeight;
  }

  private static final int DEFAULTSTATUSBARHEIGHT = -1;
  /**
   * 获取状态栏的高度
   *
   * @param context 上下文context
   * @return 状态栏的高度
   */
  public static int getStatusBarHeight(Context context) {
    int statusBarHeight = DEFAULTSTATUSBARHEIGHT;
    try {
      Class<?> clazz = Class.forName("com.android.internal.R$dimen");
      Object object = clazz.newInstance();
      int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
      statusBarHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return statusBarHeight;
  }

  /**
   * 获取屏幕截图包含状态栏
   *
   * @param activity 对应的activity
   * @return bitmap 屏幕截图
   */
  public static Bitmap screenBitmapWithStatus(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return bp;
  }

  /**
   * 获取屏幕截图不包含状态栏
   *
   * @param activity 对应的ativity
   * @return 屏幕截图
   */
  public static Bitmap screenBitmapNoStatus(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;

    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();
    return bp;
  }
}
